<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Pengangkutan extends CI_Controller {

	function __construct()
	{
		parent::__construct();	
		$this->load->helper('flexigrid');
		$this->load->library('flexigrid');
        $this->load->library('form_validation');	
		$this->load->model('model_angkut');		
	}
	
	function index() {
        $colModel['no'] = array('No', 15, TRUE, 'center', 0);
		$colModel['Edit'] = array('Edit', 50, TRUE, 'center', 0);
		$colModel['Tanggal'] = array('Tanggal', 100, TRUE, 'center', 0);
		$colModel['No_Kendaraan'] = array('No Kendaraan Pengangkut', 200, TRUE, 'center', 1);
        $colModel['Jml_angkut'] = array('Jumlah Angkutan', 150, TRUE, 'center', 1);
		  
        $gridParams = array(
            'width' => 900,
            'height' => 300,
            'rp' => 10,
            'rpOptions' => '[5,10,15,20,25,40]',
            'pagestat' => 'Menampilkan: {from} hingga {to} dari {total} data.',
            'blockOpacity' => 0.5,
            'title' => 'Daftar Pengangkutan',
            'showTableToggleBtn' => true
        );

        //$buttons[] = array('Tambah','add','test');
        //$buttons[] = array('separator');

        $buttons[] = array('Tambah', 'add', 'spt_js');
        $buttons[] = array('separator');
        $buttons[] = array('Hapus', 'delete', 'spt_js');
		$buttons[] = array('separator');
        $buttons[] = array('Select All', 'add', 'spt_js');
        $buttons[] = array('Deselect All', 'delete', 'spt_js');
        $buttons[] = array('separator');

        // mengambil data dari file controler ajax pada method grid_berkas
        $grid_js = build_grid_js('flex1', site_url("pengangkutan/pengangkutan/grid_angkut"), $colModel, 'Jumlah', 'asc', $gridParams, $buttons);


        //additional JS
        $data['added_js'] =
                "<script type='text/javascript'>
		function spt_js(com,grid)
		{
			if (com=='Tambah')
			{
				location.href='" . base_url() . "index.php/pengangkutan/pengangkutan/form_angkut'; 
			}
			
			if (com=='Select All')
				{
					$('.bDiv tbody tr',grid).addClass('trSelected');
				}
				
			if (com=='Deselect All')
				{
					$('.bDiv tbody tr',grid).removeClass('trSelected');
				}
			
			if (com=='Hapus')
				{
				   if($('.trSelected',grid).length>0){
					   if(confirm('Anda yakin menghapus ' + $('.trSelected',grid).length + ' data pengangkutan?')){
							var items = $('.trSelected',grid);
							var itemlist ='';
							for(i=0;i<items.length;i++){
								itemlist+= items[i].id.substr(3)+',';
							}
							$.ajax({
							   type: 'POST',
							   url: '" . base_url() . "index.php/pengangkutan/pengangkutan/delete" . "',
							   data: 'items='+itemlist,
							   success: function(data){
								$('#flex1').flexReload();
							   }
							});
						}
					} else {
						return false;
					} 
				}
				
		} 
		</script>
		";

        $data['js_grid'] = $grid_js;
        //remdering view
        $data['title'] = 'Daftar Pengangkutan';
        $data['content'] = $this->load->view('grid',$data,true);
		$this->load->view('main',$data);
    }
	
	function grid_angkut() {
        $valid_fields = array('Tanggal','No_Kendaraan', 'Jml_angkut');
        $this->flexigrid->validate_post('Jml_angkut', 'asc', $valid_fields);
        $records = $this->model_angkut->get_angkut_grid();
        $this->output->set_header($this->config->item('json_header'));

        $no = 0;
        foreach ($records['records']->result() as $row) {
            $link_edit = "<a href='" . base_url() . "index.php/pengangkutan/pengangkutan/edit/" . $row->ID_PENGANGKUTAN. "'>";
            $image_edit = "" . base_url() . "images/grid/edit.png";
			
			
            $record_items[] = array(
                $row->ID_PENGANGKUTAN,
                $no = $no + 1,
				'' . $link_edit . '<img border=\'0\' src=\'' . $image_edit . '\'></a> ',
				$row->TANGGAL_PENGANGKUTAN,
				$row->NO_KENDARAAN,
                $row->JML_ANGKUT.' '.$row->SATUAN
            );
        }

        if (isset($record_items))
            $this->output->set_output($this->flexigrid->json_build($records['record_count'], $record_items));
        else
            $this->output->set_output('{"page":"1","total":"0","rows":[]}');
    }
	
	 function cek_validasi() {
        // Setting Rule
        $this->form_validation->set_rules('tanggal', 'Tanggal Pengangkutan', 'required');
        $this->form_validation->set_rules('No_Kendaraan', 'No Kendaraan', 'required');
		$this->form_validation->set_rules('Jumlah', 'Jumlah Angkutan', 'required|numeric');

        // Setting Error Message
        $this->form_validation->set_message('required', 'Kolom %s harus diisi!!');
		$this->form_validation->set_message('numeric', 'Kolom %s harus diisi angka!!');

        // Setting Delimiter
        $this->form_validation->set_error_delimiters('<li class="error">', '</li>');
        return $this->form_validation->run();
    }
	
	public function form_angkut() {
        $data['status'] = 'new';
        $data['title'] = 'Tambah Data Pengangkutan';
		$data['satuan_options'] = $this->model_angkut->get_satuan();
		$data['content'] = $this->load->view('pengangkutan/form_angkut',$data,true);
		$this->load->view('main',$data);        
    }

    public function insert_angkut() {
		if ($this->cek_validasi() == TRUE) {

			$data = array(
				'TANGGAL_PENGANGKUTAN' => $this->input->post('tanggal'),
				'NO_KENDARAAN' => $this->input->post('no_kendaraan'),
				'JML_ANGKUT' => $this->input->post('jumlah'),
				'ID_SATUAN' => $this->input->post('satuan'),
			);
			$this->model_user->insert_user($data);
			redirect('pengakutan/pengangkutan');
			
			}        
                
		else {
			$this->form_angkut();
			}
	}

    public function edit($id) {
        $data['status'] = 'edit';
		$data['satuan_options'] = $this->model_angkut->get_satuan();
        $record_angkut = $this->model_angkut->selectone($id);
        foreach ($record_angkut->result() as $angkut) {
            $data['id_pengangkutan'] = $angkut->ID_PENGANGKUTAN;
            $data['tanggal'] = $angkut->TANGGAL_PENGANGKUTAN;
			$data['no_kendaraan'] = $user->NO_KENDARAAN;
            $data['jumlah'] = $user->JML_ANGKUT;
			$data['satuan'] = $user->ID_SATUAN;
        }
        $data['title'] = 'Edit Data Pengangkutan';
        $this->load->view('pengangkutan/form_angkut', $data);
   
    }

    //update peserta
    public function update_pengangkutan($id) {
			if ($this->cek_validasi() == TRUE) {
			
			$data = array(
				'TANGGAL_PENGANGKUTAN' => $this->input->post('tanggal'),
				'NO_KENDARAAN' => $this->input->post('no_kendaraan'),
				'JML_ANGKUT' => $this->input->post('jumlah'),
				'ID_SATUAN' => $this->input->post('satuan'),
			);
			$this->model_angkut->update_angkut($data, $id);
			redirect('pengangkutan/pengangkutan');
                    
            } 
			else {
			 $this->edit($id);
			}
		
	}

    public function delete($id) {
        $spt_ids_post_array = split(",", $this->input->post('items'));
        foreach ($spt_ids_post_array as $index => $id) {
            if (isset($id) && $id != '') {
                $this->model_angkut->delete_angkut($id);
            }//end if
        }//end foreach
    }
}